"use server";
import { message } from "antd";
import { getQuestionVoByIdUsingGet } from "@/api/questionController";
import QuestionCard from "@/components/QuestionCard";
import "./index.css";
import {cookies} from "next/headers";
import {AxiosResponse} from "axios";

/**
 * 题目详情页
 * @constructor
 */
export default async function QuestionPage({ params }) {
  const { questionId } = params;

  // 获取题目详情
  let question = undefined;
  try {
    // 通过 next/headers 获取当前请求的 cookie
    const cookieStore = cookies();
    const cookieHeader = cookieStore.toString();
    // 手动将 cookie 添加到请求头中
    const res: AxiosResponse = await getQuestionVoByIdUsingGet(
        { id: questionId },
        {
          headers: {
            Cookie: cookieHeader,
          },
          // 如果需要发送凭证信息，也可配置 withCredentials: true（注意：在 Node 环境下，通常需要手动设置 cookie）
          withCredentials: true,
        },
    );
    question = res.data;
  } catch (e) {
    //message.error("获取题目详情失败，" + e.message);
  }
  // 错误处理
  if (!question) {
    return <div>获取题目详情失败，请刷新重试</div>;
  }

  return (
    <div id="questionPage">
      <QuestionCard question={question} />
    </div>
  );
}
